package com.itheima.reggie.utils;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("当前线程id为:{}",Thread.currentThread().getId());
        log.info("数据插入,开始元数据对象公共字段填充{}",metaObject);
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser", BaseContext.getCurrentId());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
        log.info("元数据对象公共字段填充完毕");
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("当前线程id为:{}",Thread.currentThread().getId());
        log.info("数据更新,开始元数据对象公共字段填充{}",metaObject);
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
        log.info("元数据对象公共字段填充完毕");
    }
}
